package com.example.coursetest1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "CircleServlet", value = "/CircleServlet")
public class CircleServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 创建一个半径为3的圆
        Circle circle = new Circle(3);
        double area = circle.getArea();

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h3>半径是3的面积: " + area + "</h3>");
        out.println("</body></html>");
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取用户输入的半径
        double radius = Double.parseDouble(request.getParameter("radius"));

        // 创建一个用户输入半径的圆
        Circle circle = new Circle(radius);
        double area = circle.getArea();

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h3>半径为：" + radius + "的面积是" + area + "</h3>");
        out.println("</body></html>");
    }
}
